﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FastPoster.Helpers
{
    #region Using
    using System.Threading;
    using FastPoster.MainForms;
    using FastPoster.MainClasses;
    using FastPoster.DataProviders.BUS;
    using System.Data;
    using log4net;
using System.Windows.Forms;
    #endregion

    public class GlobalVariables
    {
        private delegate void TimeTask();
        public static string g_strPath { get; set; }
        private static IAsyncResult result;
        private static TimeTask ASynInvoke;
        public static FrmLoading waiting;
        public static List<string> LstCacBaiVietDaDang = new List<string>();
        public static List<string> LstTuKhoa = new List<string>();
        public static string BackLinksGenerated = Environment.NewLine;
        public static string END = Environment.NewLine;
        public static int Generated = 0;
        public static Dictionary<string, string> DictHtmls = new Dictionary<string, string>();
        public static readonly ILog Logger = LogManager.GetLogger(typeof(GlobalVariables).Name);
        private delegate void LoadingDel();
       
        public static void PreLoading()
        {
            ASynInvoke = new TimeTask(WaitLoad);
            result = ASynInvoke.BeginInvoke(null, null);
        }

        public static void PosLoading()
        {
            if (waiting.InvokeRequired)
            {
                LoadingDel del = new LoadingDel(PosLoading);
                waiting.Invoke(del, new object[] { });
            }
            if (waiting != null)
            {
                waiting.Close();
                waiting = null;
            }
        }

        public static void WaitLoad()
        {
            if (waiting == null)
            {
                waiting = new FrmLoading("Đang thực hiện");
            }
            if (waiting.InvokeRequired)
            {
                LoadingDel del = new LoadingDel(WaitLoad);
                waiting.Invoke(del, new object[] { });
            }
            waiting.ShowDialog();
        }

        public static List<Poster> RetrieveBoxList(int iddanhmuc)
        {
            List<Poster> list = new List<Poster>();
            var objBus = new BoxBUS(GlobalVariables.g_strPath);
            var ds = objBus.SelectByIDDanhMuc(iddanhmuc);

            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    var item = new Poster
                    {
                        Url = row["URLDienDan"].ToString(),
                        User = row["TenDangNhap"].ToString(),
                        Pass = row["MatKhau"].ToString(),
                        IDB = int.Parse(row["IDBox"].ToString()),
                        BoxLink = row["URLBox"].ToString()
                    };
                    list.Add(item);
                }
            }

            return list;
        }

        public static void CreateBackLinks()
        {
            if (LstTuKhoa.Count == 0 || LstCacBaiVietDaDang.Count == 0)
            {
                return;
            }

            BackLinksGenerated = END + END;
            BackLinksGenerated += "/*Các trang liên quan*/" + END;

            if (LstTuKhoa.Count >= LstCacBaiVietDaDang.Count)
            {
                for (int i = 0; i < LstCacBaiVietDaDang.Count; i++)
                {
                    BackLinksGenerated += "[URL=\"" + LstCacBaiVietDaDang[i] + "\"]" + LstTuKhoa[i] + "[/URL]" + END;
                }
            }
            else
            {
                if (Generated == 0)
                {
                    Generated = LstTuKhoa.Count;
                }

                int i;
                for (i = 0; i < LstTuKhoa.Count; i++)
                {
                    if (i + Generated >= LstCacBaiVietDaDang.Count)
                    {
                        var pos = i;
                        int j;
                        for (j = 0; j < LstTuKhoa.Count - i; j++)
                        {
                            BackLinksGenerated += "[URL=\"" + LstCacBaiVietDaDang[j] + "\"]" + LstTuKhoa[pos] + "[/URL]" + END;
                            pos++;
                        }
                        Generated = j;
                        return;
                    }
                    else
                    {
                        BackLinksGenerated += "[URL=\"" + LstCacBaiVietDaDang[i + Generated] + "\"]" + LstTuKhoa[i] + "[/URL]" + END;
                    }
                }

                Generated = i + Generated;
            }
        }
    }
}
